public member function
<iterator>

std::move_iterator::operator[]

/*unspecified*/ operator[] (difference_type n) const;
Dereference iterator with offset
Returns an rvalue reference to the element located n positions away from the element currently pointed to by the iterator.

Internally, the function returns the result of dereferencing its base iterator with the same offset casted to the appropriate rvalue reference type.

If such an element does not exist, it causes undefined behavior.

Parameters

n
Number of elements to offset.
Member type difference_type is an alias of the base iterator's own difference type.

Return value

An rvalue reference to the element n positions away from the element currently pointed by the iterator.
Its type is the rvalue reference type of the one returned by invoking the same operator on the base iterator.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// move_iterator::operator[] example
#include <iostream>     // std::cout
#include <iterator>     // std::move_iterator
#include <vector>       // std::vector
#include <string>       // std::string

int main () {
  std::string str[] = {"one","two","three"};
  std::vector<std::string> foo;

  std::move_iterator<std::string*> it (str);

  for (int i=0; i<3; ++i)
    foo.push_back(it[i]);

  std::cout << "foo:";
  for (std::string& x : foo) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}

Output:

foo: one two three


Data races

The object is accessed.
The reference returned can be used to access or modify elements.

Exception safety

Provides the same level of guarantee as the operations internally applied to the base iterator.

See also